<template>
    <div class="mod-prod-info">
      <el-button type="primary" style="margin-bottom: 10px"
          @click="dataFormSubmit()">保存
      </el-button>
        <el-form ref="dataForm"
                 :model="tprojectMain"
                 label-width="100px">
            <el-row>
                <el-col :span="24"></el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item :rules="[
                            { required: true, message: '来料厂家不能为空'}
                          ]"
                                  label="来料厂家"
                                  prop="llcj">

                        <el-input v-model="tprojectMain.llcj"
                                  maxlength="50"
                                  placeholder="来料厂家"></el-input>

                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item :rules="[
                            { required: true, message: '实验温度不能为空'}
                          ]"
                                  label="实验温度"
                                  prop="sywd">

                        <el-input v-model="tprojectMain.sywd"
                                  maxlength="100"
                                  placeholder="实验温度"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="来料日期" prop="llrq" :rules="[
                            { required: true, message: '来料日期不能为空'}
                          ]">

                        <el-date-picker v-model="tprojectMain.llrq"
                                  type="date"
                                  value-format="yyyy-MM-dd"></el-date-picker>

                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="测试日期" prop="csrq" :rules="[
                            { required: true, message: '测试日期不能为空'}
                          ]">
                      
                      <el-date-picker v-model="tprojectMain.csrq"
                                      type="date"
                                      value-format="yyyy-MM-dd"></el-date-picker>

                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="单价"  :rules="[
                            { required: true, message: '单价不能为空'}
                          ]">

                        <el-input v-model="tprojectMain.dj"
                                  placeholder=""
                                  step="1"
                                  type="number">
                        </el-input>

                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="部门主管" prop="bmzg"  :rules="[
                            { required: true, message: '部门主管不能为空'}
                          ]">
                        <el-col>
                            <el-input v-model="tprojectMain.bmzg"
                                      placeholder="部门主管"
                                      type="input"></el-input>
                        </el-col>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item :rules="[
                      { required: true, message: '操作者不能为空'}
                    ]"
                                  label="操作者"
                                  prop="czz">

                        <el-input v-model="tprojectMain.czz"
                                  maxlength="100"
                                  placeholder=""></el-input>

                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备注" prop="biz">
                        <el-input v-model="tprojectMain.biz"
                                  maxlength="20"
                                  placeholder=""></el-input>

                    </el-form-item>
                </el-col>
            </el-row>
            <!--
			<el-form-item>
				<el-button
					@click="addCert()">增加许可证</el-button>
			  <el-button
				  @click="saveCert()">保存许可证</el-button>
			  <projectprecert-table  ref="projectPrecerttable"  v-model="tProjectPresalecerts" >
			  </projectprecert-table>
			</el-form-item>
				-->
            <el-row>
                <el-col :span="24">
                    <el-form-item>
                        <el-button style="margin-bottom: 10px"
                            @click="addBuild()">增加产品信息
                        </el-button>
                        
                        <projectbuilding-table ref="projectBuildingtable" v-model="tProjectBuildings">
                        </projectbuilding-table>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="24">
                    <el-form-item>
                        <el-input v-model="tprojectMain.pdjg"
                                  maxlength="20"
                                  placeholder="判定结果"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <projectbuilding v-if="projectbuildingVisible" ref="buildmod" @refreshBuilding="addProduct">
        </projectbuilding>
        <projectcert v-if="projectcertVisible" ref="certmod" @refreshCert="getDataList">
        </projectcert>

    </div>


</template>

<script>
import MulPicUpload from '@/components/mul-pic-upload'
import TinyMce from '@/components/tiny-mce'
import projectprecertTable from './projectprecert-table'
import projectbuildingTable from './projectbuilding-table'
import projectbuilding from './projectbuilding'
import {Debounce} from '@/utils/debounce'
import {getUUID} from '@/utils'
import projectcert from './projectcert'

export default {
	data () {
		return {
			mainId: '',
			projectbuildingVisible: false,
			projectcertVisible: false,
			// 项目信息列表
			tprojectMain: {
				mainId: '',
				llcj: '',
				sywd: '',
				csrq: '',
				dj: 0,
				bmzg: '',
				czz: '',
                pdjg: '',
				llrq: '',
                biz: ''
			},
			tProjectBuildings: [],
			tProjectPics: [],
			tProjectPresalecerts: [],
			dataForm: {},
			tags: [],
			resourcesUrl: process.env.VUE_APP_RESOURCES_URL
		}
	},
	components: {
		projectcert,
		MulPicUpload,
		TinyMce,
		projectprecertTable,
		projectbuildingTable,
		projectbuilding
	},
	computed: {},
	activated () {
		console.info('mainId')
		console.info(this.$route.query.mainId)
        if(this.$route.query.mainId){
            this.tprojectMain.mainId = this.$route.query.mainId
            this.getDataList()
        }else{
            this.tprojectMain.mainId = getUUID()
        }
		
		console.info(this.tprojectMain.mainId)
		
	},
	methods: {
		// 获取分类数据
		getDataList () {
			console.info('this.tprojectMain')
			console.info(this.tprojectMain)
			if (this.tprojectMain.mainId) {
				// 获取项目数据
				this.$http({
					url: this.$http.adornUrl(`/crm/jcbg/getJcbgMainById`),
					method: 'get',
					params: this.$http.adornParams({
                        id: this.tprojectMain.mainId
                    })
				}).then(({data}) => {
					console.info('数据....')
					console.info(data)
					this.tprojectMain = data.obj
					this.tProjectBuildings = data.obj.crmJcbgDtlList
					// this.tProjectPresalecerts = data.tProjectPresalecerts
				})
			} else {
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
				})
			}
		},
		getBuildList () {
			this.$http({
				url: this.$http.adornUrl(`/project/getProjectBuildinglist`),
				method: 'post',
				params: this.$http.adornParams(
					{
						mainId: this.dataForm.mainId
					}
				)
			}).then(({data}) => {
				this.dbSpecs = data
			}).catch(({data}) => {
				console.info(data)
			})
		},
		getCertList () {
			let param = {mainId: this.dataForm.mainId || 0}
			console.info('子组件的获取参数')
			console.info(param)
			this.$http({
				url: this.$http.adornUrl(`/project/getProjectPresalecertlist`),
				method: 'post',
				params: this.$http.adornParams(
					param
				)
			}).then(({data}) => {
				this.tProjectPresalecerts = data.data
			}).catch(
				({data}) => {
					console.info(data)
				})
		},
		addBuild () {
			this.projectbuildingVisible = true
			this.$nextTick(() => {
				this.$refs.buildmod.init(this.dataForm.mainId)
			})
		},
        addProduct(data){
            console.log('🚀 ~ data', data)
            this.$refs.projectBuildingtable.handleAdd(data)
        },
		addCert () {
			this.projectcertVisible = true
			this.$nextTick(() => {
				this.$refs.certmod.init(this.dataForm.mainId)
			})
		},
		saveBuild () {
            console.log('🚀 ~ tProjectBuildings', this.tProjectBuildings)
		},
		saveCert () {
			console.info(this.tProjectPresalecerts)
			this.$http({
				url: this.$http.adornUrl('/project/savePresalecerts'),
				method: 'post',
				params: this.$http.adornParams(
					this.tProjectPresalecerts
				)
			}).then(({data}) => {
				console.info(data)
				this.getDataList()
			})
		},
		// 表单提交
		dataFormSubmit: Debounce(function () {
			this.$refs['dataForm'].validate((valid) => {
				if (!valid) {
					return
				}
				if (this.tProjectBuildings.length === 0) {
					this.errorMsg('请至少添加一条产品信息')
					return
				}
				// 参数进行保存
				let paramMain = Object.assign({}, this.tprojectMain)

				//如果mainId为空则赋值一个新的mainId走保存模式
				if (!paramMain.mainId) {
					paramMain.mainId = getUUID()
				}
				

				// 设置价格和库存
				this.$http({
					url: this.$http.adornUrl('/crm/jcbg/saveJcbgAllInfo'),
					method: 'post',
					data: this.$http.adornData(
                        {
                            id: this.tprojectMain.mainId,
                            llcj: this.tprojectMain.llcj,
                            sywd: this.tprojectMain.sywd,
                            csrq: this.tprojectMain.csrq,
                            dj: this.tprojectMain.dj,
                            bmzg: this.tprojectMain.bmzg,
                            czz: this.tprojectMain.czz,
                            pdjg: this.tprojectMain.pdjg,
                            llrq: this.tprojectMain.llrq,
                            biz: this.tprojectMain.biz,
                            crmJcbgDtlList: this.tProjectBuildings
                        }
					)
				}).then(({data}) => {
					console.info(data)
					if(data.msg === "success"){
                        this.$message.success("保存成功")
                        this.$router.go(-1)
                    }
				})
			})
		}),
		errorMsg (message) {
			this.$message({
				message: message,
				type: 'error',
				duration: 1500
			})
		}
	}
}
</script>
